<template>
  <el-dialog
    :model-value="dialogVisibles"
    :title="dialogTitles"
    width="70%"
    @close="handleClose"
  >
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="选择配件分类" prop="id">
        <el-select v-model="queryForm2.id" placeholder="请选择" @click="query2">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            @click="query3">
          </el-option>
        </el-select>
      </el-form-item>
      <el-row class="header">
        <el-table ref="multipleTable" :data="tableData" row-key border style="width: 100%" @select="select"
                  select-on-indeterminate="true">
<!--          <el-table-column type="selection" width="55"/>-->
          <el-table-column prop="accessories.name" label="配件名称" />
          <el-table-column prop="accessories.total" label="配件库存可用数" />
          <el-table-column prop="accessories.price" label="配件单价" />
          <el-table-column label="操作" width="180">
            <template #default="{ row }">
              <el-button type="primary" @click="maintenance(row)">选择配件</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </el-form>
    <Appointment v-model="appointmentVisible"
                 :appointmentTitle="appointmentTitle"
                 v-if="appointmentVisible"
                 :appointmentRowValue="appointmentRowValue"
                 :appointmentRowValues="appointmentRowValues" />
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch, onMounted } from 'vue'
import { listAccessType } from '../../../api/accessoriesType'
import { lists } from '../../../api/inventory'
import Appointment from './appointment'

const form = ref({
  id: '',
  maintainId: ''
})

// 配件分类
const queryForm2 = ref({
  id: '',
  name: ''
})

const props = defineProps({
  // 标题
  dialogTitles: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValues: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValues,
  () => {
    // console.log(props.dialogRowValue)
    form.value = props.dialogRowValues
  },
  { deep: true, immediate: true }
)

const isDisabled = ref(false)
onMounted(() => {
  if (props.dialogTitle === '编辑') {
    isDisabled.value = true
  }
})

// 多选框拿数据
function select(selection, row) {
  form.value = JSON.parse(JSON.stringify(selection))
}

const tableData = ref([])

const dialogVisibles = ref(false)
const total = ref(0)
const appointmentVisible = ref(false)
const appointmentTitle = ref('')
const appointmentRowValue = ref({})
const appointmentRowValues = ref({})

// 配件数量
function maintenance(row) {
  appointmentTitle.value = '配件数量'
  appointmentRowValue.value = JSON.parse(JSON.stringify(row))
  appointmentRowValues.value = form.value.id // 获取保养项目的ID
  appointmentVisible.value = true
}

// 配件分类
const options = ref([])
async function query2() {
  const row = await listAccessType()
  queryForm2.value = row.data.data
  options.value = queryForm2.value
}

async function query3() {
  const resp = await lists(queryForm2.value.id)
  tableData.value = resp.data.data.list
  total.value = resp.data.data.total
}

// 表单校验
const rules = ref({
  type: [
    { required: true, message: '门店名称不能为空' }
  ]
})

const emits = defineEmits(['update:modelValue', 'query'])

// 关闭
function handleClose() {
  emits('update:modelValue', false)
}
</script>

<style lang="scss" scoped>
::v-deep .el-input__prefix-inner {
  align-items: center;
}
</style>
